package com.ruoyi.web.controller.lucky;

import java.util.List;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.lucky.domain.TBoxGoods;
import com.ruoyi.lucky.service.ITBoxGoodsService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 盲盒商品关联表Controller
 * 
 * @author liuhongbin
 * @date 2020-11-15
 */
@Controller
@RequestMapping("/lucky/boxGoods")
public class TBoxGoodsController extends BaseController
{
    private String prefix = "lucky/goods";

    @Autowired
    private ITBoxGoodsService tBoxGoodsService;

    @RequiresPermissions("lucky:goods:view")
    @GetMapping()
    public String goods()
    {
        return prefix + "/goods";
    }

    /**
     * 查询盲盒商品关联表列表
     */
    @RequiresPermissions("lucky:goods:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(TBoxGoods tBoxGoods)
    {
        startPage();
        List<TBoxGoods> list = tBoxGoodsService.selectTBoxGoodsList(tBoxGoods);
        return getDataTable(list);
    }

    @PostMapping("/list/{id}")
    @ResponseBody
    public TableDataInfo list(@PathVariable("id") Long id, ModelMap mmap)
    {
        startPage();
        TBoxGoods tBoxGoods=new TBoxGoods();
        tBoxGoods.setBlindboxId(id);
        List<TBoxGoods> tBoxGoodsList=tBoxGoodsService.selectTBoxGoodsList(tBoxGoods);
        return getDataTable(tBoxGoodsList);
    }


    /**
     * 导出盲盒商品关联表列表
     */
    @RequiresPermissions("lucky:goods:export")
    @Log(title = "盲盒商品关联表", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(TBoxGoods tBoxGoods)
    {
        List<TBoxGoods> list = tBoxGoodsService.selectTBoxGoodsList(tBoxGoods);
        ExcelUtil<TBoxGoods> util = new ExcelUtil<TBoxGoods>(TBoxGoods.class);
        return util.exportExcel(list, "goods");
    }

    /**
     * 新增盲盒商品关联表
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存盲盒商品关联表
     */
    @RequiresPermissions("lucky:goods:add")
    @Log(title = "盲盒商品关联表", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(TBoxGoods tBoxGoods)
    {
        return toAjax(tBoxGoodsService.insertTBoxGoods(tBoxGoods));
    }

    /**
     * 修改盲盒商品关联表
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Integer id, ModelMap mmap)
    {
        TBoxGoods tBoxGoods = tBoxGoodsService.selectTBoxGoodsById(id);
        mmap.put("tBoxGoods", tBoxGoods);
        return prefix + "/edit";
    }

    /**
     * 修改保存盲盒商品关联表
     */
    @RequiresPermissions("lucky:goods:edit")
    @Log(title = "盲盒商品关联表", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(TBoxGoods tBoxGoods)
    {
        return toAjax(tBoxGoodsService.updateTBoxGoods(tBoxGoods));
    }

    /**
     * 删除盲盒商品关联表
     */
    @RequiresPermissions("lucky:goods:remove")
    @Log(title = "盲盒商品关联表", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(tBoxGoodsService.deleteTBoxGoodsByIds(ids));
    }


    /**
     * 盲盒商品选择页面
     */
    @GetMapping("/selectGoods")
    public String selectGoods(ModelMap mmap,Long blindboxId) {
        //查询成员组选中人员返回
        TBoxGoods tBoxGoods=new TBoxGoods();
        tBoxGoods.setBlindboxId(blindboxId);
        List<TBoxGoods> tBoxGoodsList=tBoxGoodsService.selectTBoxGoodsList(tBoxGoods);
        Long[] selectIds=new Long[tBoxGoodsList.size()];
        if(!CollectionUtils.isEmpty(tBoxGoodsList)){
            int i=0;
            for(TBoxGoods boxGoods:tBoxGoodsList){
                selectIds[i] = boxGoods.getGoodsId();
                i = i+1;
            }
        }
        mmap.put("blindboxId",blindboxId);
        mmap.put("selectIds",selectIds);
        return   prefix+"/selectGoods";
    }
}
